//index.js
//获取应用实例
var app = getApp()
Page({


  data: {
    album_type:1,
    domain:app.globalData.domain,
    System_Img_Url:app.globalData.System_Img_Url

  },
  
  onLoad: function () {
    var t = this
    wx.getSystemInfo({
      success: function(res) {
        t.setData({
        //  s_height:res.screenHeight,
          get_height:res.screenHeight
        });
      }
    })

    this.Get_All_Public_Album(1,0,this.data.album_type)
    this.Get_Collect_Album()
  },




  //下拉刷新 
  onReachBottom(){
    this.Get_All_Public_Album();
  },



//缓存搜索值
  formSubmit: function(e) {
    var search = e.detail.value.search;
    this.setData({
      search:search,
      pnow:'',
      album_info_arr:''
    });

    this.Get_All_Public_Album();
    this.Get_Collect_Album()

  },


//获取全部收藏相册id
  Get_Collect_Album(){
    var t = this
    var openid = wx.getStorageSync('openid');
    wx.request({
      url:app.globalData.urls,
      data:{
        GetTypes:'Get_Collect_Albums2',
        openid:openid
      },
      success: function(res) {
        var Aids = new Array();
        for(var i=0;i<res.data.length;i++){
          Aids.push(res.data[i].Collect_Aid)
        }
        t.setData({
          Aids:Aids
        });
        wx.setStorageSync('collect_aids',Aids);
      }
    });
  },


//获取全部公共相册
  Get_All_Public_Album(pnow=1,pcount=0,type=1){
    var t = this
    if(t.data.pnow){
      if(t.data.pcount >= t.data.pnow){
        var cpnow = t.data.pnow;
      }else{
        return;
      }
    }else{
      var cpnow = 1;
    }
    if(t.data.search){
      var search=t.data.search.trim();
    }else{
      var search='';
    }
    wx.request({
      url:app.globalData.urls,
      data:{
        GetTypes:'Get_All_Public_Album',
        pnow:cpnow,
        pcount:pcount,
        search:search,
        type:type
      },
      success: function(res) {
       // console.log(res.data)
        if(!t.data.album_info_arr){
          var album_info_arr = new Array();
        }else{
          var album_info_arr = t.data.album_info_arr
        }

        album_info_arr.push(res.data);
      //  console.log(album_info_arr)

        //判断当前面数，大于1页就设置动态高度
        if(res.data.PageCount > 1){
          var get_height_str = 'auto';
        }else{
          var get_height_str = t.data.get_height;
        }
        t.setData({
          pnow:parseInt(res.data.Pnow)+1,
          pcount:parseInt(res.data.PageCount),
          album_info:res.data,
          album_info_arr:album_info_arr,
          s_height:get_height_str
        })
      }
    })
  },



//跳转到图片
  Jump_Pics(e){
    var album_id = e.currentTarget.dataset.album_id
    var album_name = e.currentTarget.dataset.album_name
    var album_cover = e.currentTarget.dataset.album_cover
    var album_flower = e.currentTarget.dataset.album_flower
    var album_collect = e.currentTarget.dataset.album_collect
    var album_openid = e.currentTarget.dataset.album_openid
    wx.navigateTo({
      url:'../pics/pics?album_id='+album_id+'&album_name='+album_name+'&album_cover='+album_cover+'&album_flower='+album_flower+'&album_collect='+album_collect+'&album_openid='+album_openid,
    })
  },


//点赞
  Zhan(e){
    var aid = e.currentTarget.dataset.aid;
    var that = this
    wx.request({
      url:app.globalData.urls,
      data:{
        GetTypes:"Album_Flower_Add",
        Openid:wx.getStorageSync('openid'),
        Album_Id:e.currentTarget.dataset.aid
      },
      success:function(res){
        if(res.data == 'ok'){
          var tmp_data = that.data.album_info_arr
          for(var i=0;i<tmp_data.length;i++){
            for(var j=0;j<tmp_data[i].DataList.length;j++){
              if(tmp_data[i].DataList[j].Album_Id == aid){
                tmp_data[i].DataList[j].Album_Flower_Count = tmp_data[i].DataList[j].Album_Flower_Count+1
                var breaks = true;
                break
              }
            }
            if(breaks){
              break;
            }
          }
          that.setData({
            album_info_arr:tmp_data
          })
        }
      }

    })
  },


//收藏 
  Collect(aid){
    
    var t = this
    wx.request({
      url:app.globalData.urls,
      data:{
        GetTypes:"Collect_Add",
        Openid:wx.getStorageSync('openid'),
        Album_Id:aid
      }
    })

  },


  Set_Collect_Stat(e){
   // console.log(this.data)
    var aid = e.currentTarget.dataset.aid
    this.Collect(aid);
    var data = this.data.Aids
    //console.log(aid)
    //console.log(data)
    var stat = false;
    for(var i=0;i<data.length;i++){
      if(data[i] == aid){
        data.splice(i,1);
        stat = true
        break;
      }
    }
    if(!stat){
      data.push(aid)
    }
    this.setData({
      Aids:data
    })
   
  }


})
